#!/usr/bin/python
# -*- coding: latin-1 -*-
# This is the gui for the script - tomtom.py
# Copyright (C) <2007>  <Callous Crab>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Contact information:
# Callous Crab <callouscrab+tomtompy@gmail.com>


import os
from Tkinter import *
import tkFileDialog

import tomtom

class App:
    def __init__(self, master):
        frame = Frame(master)
        frame.grid()
        self.itn_name_var = StringVar()
        self.dirname = StringVar()
        self.dirname.set(os.getcwd())
        self.done_var = StringVar()

        self.itn_label = Label(master, text="Enter Itinerary Name: ",
                               width=30)
        self.url_label = Label(master, text="Enter URL from Google Maps: ",
                               width=30)
        self.itn_button = Button(master, text="Select directory",
                               command=self.browse_itn)
        self.itn_path = Label(master, textvariable=self.dirname, width=30)
        self.done = Label(master, textvariable=self.done_var, width=10,
                              bg='#0ff')
        

        self.itn_name = Entry(master, textvariable=self.itn_name_var)
        self.url = Text(master, height=4, width=40)

        self.quit_button = Button(master, text='Quit', command=frame.quit)
        self.exec_button = Button(master, text='Get tomtom',
                                  command=self.get_params)

        self.itn_label.grid(row=1, sticky=W)        
        self.itn_name.grid(row=1, column=1, sticky=W)
        self.url_label.grid(row=2, sticky=W)
        self.url.grid(row=2, column=1)
        self.itn_button.grid(row=3, sticky=E)
        self.itn_path.grid(row=3, column=1)
        self.quit_button.grid(row=4, column=2, sticky=E)
        self.exec_button.grid(row=4, column=0, sticky=W)


    def browse_itn(self):
        self.dirname.set(tkFileDialog.askdirectory(initialdir="/home/",
                                      title='Please select a directory'))
        return

    def get_params(self):
        itn = self.itn_name_var.get()
        url = self.url.get(1.0, END)

        tomtom.get_itn(url, itn, self.dirname.get())
        f = os.path.join(self.dirname.get(), itn) + '.zip'
        if (os.path.isfile(f)):
            self.done_var.set('Done')
        else:
            self.done_var.set('Error')
        self.done.grid(row=4, column=1)
            


root = Tk()
app = App(root)
root.title('Google Maps to TomTom GO 510')
root.mainloop()
